<template>
	<view>


		<swiper-tab-head :tabIndex='tabIndex' :tabs='tabBar' :navScrollLeft='navScrollLeft'
			@tab-click='handleTabItemClick' />

		<view class="uni-tab-bar">
			<swiper class="swiper-box" :current='tabIndex' @change="handleSwiperChange"
				:style="{'height': swiperHeight + 'px'}">
				<swiper-item v-for="(items, index) in topiclist" :key='index'>
					<scroll-view scroll-y class="list" @scrolltolower="handleScrolltolower(items)">
						<template v-if="items.list.length">
							<!-- 话题列表 -->
							<view class="topic-view">
								<block v-for="(item, index1) in items.list" :key='index1'>
									<topic-list :item='item' :index="index"/>
								</block>
							</view>
							
							<!-- 上拉加载更多 -->
							<load-more :loadtext='items.loadtext' />
						</template>

						<!-- 无内容默认 -->
						<template v-else>
							<no-thing />
						</template>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>

	</view>
</template>

<script>
	import SwiperTabHead from '../../components/index/swiper-tab-head.vue'
	import LoadMore from '../../components/common/load-more.vue'
	import NoThing from '../../components/common/no-thing.vue'
	import TopicList from '../../components/news/topic-list.vue'
	export default {
		components: {
			TopicList,
			SwiperTabHead,
			LoadMore,
			NoThing
		},
		data() {
			return {
				swiperHeight: 500,
				windowWidth: 0,
				tabIndex: 0,
				navScrollLeft: 0,
				tabBar: [{
						name: "关注",
						id: "guanzhu"
					},
					{
						name: "军事",
						id: "junshi"
					},
					{
						name: "推荐",
						id: "tuijian"
					},
					{
						name: "体育",
						id: "redian"
					},
					{
						name: "财经",
						id: "caijing"
					},
					{
						name: "娱乐",
						id: "yule"
					}
				],

				topiclist: [{
						loadtext: '上拉加载更多',
						list: [
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							},
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							},
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							}
						]
					},
					{
						loadtext: '上拉加载更多',
						list: [
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							},
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							},
							{
								toppic: '/static/demo/topicpic/13.jpeg',
								title: '我是话题',
								desc: '我是话题描述',
								totalnum: 50,
								todaynum: '10'
							}
						]
					},
					{
						loadtext: '上拉加载更多',
						list: []
					},
					{
						loadtext: '上拉加载更多',
						list: []
					},
					{
						loadtext: '上拉加载更多',
						list: []
					},
					{
						loadtext: '上拉加载更多',
						list: []
					}
				]

			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: (res) => {
					let height = res.windowHeight - uni.upx2px(100)
					this.windowWidth = res.windowHeight
					this.swiperHeight = height
				}
			})
		},
		methods: {
			// 点击tab项(index下标)
			handleTabItemClick(index) {
				this.tabIndex = index
			},
			// 轮播图变化(获取index)
			handleSwiperChange(e) {
				this.tabIndex = e.detail.current
				let singleNavWidth = this.windowWidth / 5;
				this.navScrollLeft = (this.tabIndex - 2) * singleNavWidth
			},
			// 上拉加载更多
			handleScrolltolower(row) {
				if (row.loadtext != '上拉加载更多') return
				row.loadtext = '正在加载中.....'

				setTimeout(() => {
					row.list = row.list.concat(...row.list)
					row.loadtext = '上拉加载更多'
				}, 1000)
			},
			// 点击原生按钮取消
			onNavigationBarButtonTap(e) {
				switch(e.index) {
					case 1: 
					uni.navigateTo({
						url:"/pages/publish/publish"
					})
					break;
				}
			},
			// 点击原生输入框
			onNavigationBarSearchInputClicked() {
				uni.navigateTo({
					url:'/pages/search/search'
				})
			}
		}
	}
</script>

<style scoped>
	.topic-view {
		padding: 0 10upx;
	}
</style>
